



//
//  NProfileModifyViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/25.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NProfileModifyViewController.h"
#import "NMineCell.h"
#import "NChangePWDViewController.h"
#import "NChangeNickNameViewController.h"
#import "ZFImagePicker.h"
#import "ZFActionSheet.h"

@interface NProfileModifyViewController ()<UITableViewDelegate,UITableViewDataSource,ZFActionSheetDelegate,ZFImagePickerDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *titles;
@property (nonatomic,strong)NSArray *picArray;
@property (nonatomic,strong)UIImageView *headImg;
@property (nonatomic,copy)NSString *headImgUrl;
@property (nonatomic,copy)NSString *nickName;
@property (nonatomic,copy)NSString *userInfoName;

@end

#define cellIdentifier @"mineid"
#define SectionHeaderHeight  15.f

@implementation NProfileModifyViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMineCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"基本信息" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    NSString *usrNm = self.userName;
    
    self.picArray = @[@"head",@"headicon",@"headicon",@"lock"];
    
    self.titles = @[usrNm?usrNm:@"",@"用户名",@"昵称",@"修改密码"];
    
    [self.view addSubview:self.tableView];
    
//    [self loadUserData];
    // Do any additional setup after loading the view.
}

- (void)loadUserData{
    
    //
    
    WeakStament(ws);
    
    NSDictionary *dic = @{@"userInfoId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@""};
    
    [[NNRCNetRequest manager]getWithUrl:kUserInfoUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        
        if ([result[@"code"] integerValue]==0) {
            self.headImgUrl = result[@"page"][@"userInfoUrl"];
            self.nickName = result[@"page"][@"userInfoNickname"];
            self.userInfoName = result[@"page"][@"userInfoName"];
            [ws.tableView reloadData];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
    }];
}



- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];

    [self loadUserData];
    
    NSString *url = [[NSUserDefaults standardUserDefaults]objectForKey:@"head"];
    
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:url]];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        return 90.f;
        
    }else{
        
        return 55.f;
        
    }
}

#pragma mark - datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NMineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    [cell configWithPicName:self.picArray[indexPath.section] title:self.titles[indexPath.section] withIndexPath:indexPath];
    
    if (indexPath.section==0) {
        [cell.LogoImage sd_setImageWithURL:[NSURL URLWithString:self.headImgUrl]];
        cell.setLabel.hidden = YES;
        cell.arrowImg.hidden = false;
    }else if (indexPath.section==1){
        //用户名
        cell.setLabel.hidden = false;
        cell.setLabel.text = self.userInfoName;
        cell.arrowImg.hidden = YES;
        
    }else if (indexPath.section==2){
        //昵称
        cell.setLabel.text = [NUserModel shareInstance].userInfoNickname;
        cell.arrowImg.hidden = YES;
        cell.setLabel.hidden = false;
        
    }else{
        cell.arrowImg.hidden = false;
        cell.setLabel.hidden = YES;
    }
    
    return cell;
}

#pragma mark - delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
            
        case 0:
        {
            
            ZFActionSheet *sheet = [ZFActionSheet actionSheetWithTitle:nil confirms:@[@"相册"] cancel:@"取消" style:ZFActionSheetStyleDefault];
            sheet.delegate = self;
            [sheet show];
        }
            break;
            
        case 1:
        {
            
        }
            break;
            
        case 2:
        {
            NChangeNickNameViewController *nick = [NChangeNickNameViewController new];
            nick.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:nick animated:YES];
        }
            break;
            
        case 3:
        {
            NChangePWDViewController *pwd = [NChangePWDViewController new];
            pwd.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:pwd animated:YES];
            
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - zfimage  delegate
- (void)clickAction:(ZFActionSheet *)actionSheet atIndex:(NSUInteger)index
{
    
    ZFImagePicker *picker = [[ZFImagePicker alloc] init];
    //    picker.pickerDelegate = self;
    picker.isEdit = YES;//解决拍照图片逆时针旋转90
    if (index==0) {
//        if (TARGET_IPHONE_SIMULATOR) {
//            [ToolClass showAlertWithMessage:@"请使用真机"];
//            return;
//        }
        picker.sType = SourceTypeCamera;
    }else if(index==1){
        //图片库
        picker.sType = SourceTypeLibrary;
        
    }else{
        picker.sType = SourceTypeAlbum;
    }
    
    // 实现block回调
    WeakStament(wself);
    
    picker.pickImage = ^(UIImage *image,NSString *type,NSString *name){
        
        YYLog(@"\nimage:%@\n type:%@",image,type);
        
        NSArray *array = @[image];
        
        NSDictionary *dict = @{@"userInfoId":[NUserModel shareInstance].userInfoId?:@""};
        
        [NNetworkClient uploadImage:array url:kUploadHeadImgUrl parameters:dict success:^(id responseObject) {
            
            YYLog(@"%@",responseObject);
            //
            if ([responseObject[@"code"] isEqualToString:@"0"]) {
                
                //接收返回的url 进行回显
                NSString *url = responseObject[@"page"];
                self.headImgUrl = url;
                [self.tableView reloadData];
//                [[NSUserDefaults standardUserDefaults]setObject:url forKey:@"head"];
//                [[NSUserDefaults standardUserDefaults]synchronize];
                
//                [self.headImg sd_setImageWithURL:[NSURL URLWithString:url]];
            }
            
        } fail:^(NSError *error) {
            
            YYLog(@"%@",error);
            
        } delegater:nil];
        //        [[SDImageCache sharedImageCache]clearMemory];
        //        [[SDImageCache sharedImageCache]clearDisk];
        //
        //        NSData  *imageData = UIImageJPEGRepresentation(image, 0.1);
        //        NSString  *base64ImageString = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        //        NSDictionary *para = @{@"headImg":base64ImageString};
        //        [WDNetworkClient postRequestWithBaseUrl:kUploadHeadImageUrl setParameters:para success:^(id responseObject) {
        //
        //            YYLog(@"%@",responseObject);
        //            if ([(responseObject[@"result"][@"code"]) isEqualToString:@"1000"]) {
        //
        //                NSString *headImageUrl = responseObject[@"data"][@"headImg"];
        //
        //                //                [[WDUserInfoModel shareInstance] clearHeadImage];
        //                [WDUserInfoModel shareInstance].headImg = headImageUrl;
        //                wself.headerView.headerImage.image = image;
        //                [ToolClass showAlertWithMessage:@"头像修改成功"];
        //            }
        //
        //        } fail:^(NSError *error) {
        //
        //            YYLog(@"%@",error);
        //
        //        } delegater:nil];
    };
    // 4、弹出界面
    [self presentViewController:picker animated:YES completion:nil];
    
}



@end
